using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Drawing;

public partial class Admin_gallery : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            loadCategories();
        }
    }

    private void loadCategories()
    {
        DirectoryInfo dirGallery = new DirectoryInfo(Server.MapPath("../img/gallery"));
        DirectoryInfo[] dirsCategories = dirGallery.GetDirectories();
        foreach (DirectoryInfo dirCategory in dirsCategories)
        {
            cmbCategories.Items.Add(new ListItem(dirCategory.Name));
        }
    }
    protected void btnProcess_Click(object sender, EventArgs e)
    {
        try{if (chkOptimize.Checked) optimizeImages();}
        catch{lblMessage.Text = " Error:Optimization ";}
        try{createThumbnails();}
        catch{lblMessage.Text = " Error:Thumbnails ";}
        lblMessage.Text = " Completed <a href='../gallery.aspx?cat="+cmbCategories.SelectedItem.Text+"' target='_blank'>View</a>";
    }

    private void optimizeImages()
    {
        string strBaseDir = "../img/gallery/";
        if (cmbCategories.SelectedItem == null)
            return;
        string strCategory = cmbCategories.SelectedItem.Text + "/";
        string strFileName = "";
        DirectoryInfo dirCategory = new DirectoryInfo(Server.MapPath(strBaseDir + strCategory));
        FileInfo[] files = dirCategory.GetFiles();

        foreach (FileInfo file in files)
        {
            if (file.Extension.ToLower() == ".jpg" || file.Extension.ToLower() == ".bmp")
            {
                strFileName = file.Name;
                System.Drawing.Image image = System.Drawing.Image.FromFile(Server.MapPath(strBaseDir + strCategory + strFileName));
                System.Drawing.Imaging.ImageFormat imageFormat = image.RawFormat;
                if (image.Width > image.Height)
                {
                    if (image.Width > 700)
                    {
                        System.Drawing.Image optImage = image.GetThumbnailImage(600, (600 * image.Height) / image.Width, null, new IntPtr());
                        image.Dispose();
                        optImage.Save(Server.MapPath(strBaseDir + strCategory + strFileName), imageFormat);
                    }
                }
                else
                {
                    if (image.Height > 500)
                    {
                        System.Drawing.Image optImage = image.GetThumbnailImage((400 * image.Width) / image.Height, 400, null, new IntPtr());
                        image.Dispose();
                        optImage.Save(Server.MapPath(strBaseDir + strCategory + strFileName), imageFormat);
                    }
                }
            }
        }
    }

    private void createThumbnails()
    {
        string strBaseDir = "../img/gallery/";
        if (cmbCategories.SelectedItem == null)
            return;
        string strCategory = cmbCategories.SelectedItem.Text+"/";
        DirectoryInfo dirCategory = new DirectoryInfo(Server.MapPath(strBaseDir + strCategory));
        FileInfo[] files = dirCategory.GetFiles();

        foreach (FileInfo file in files)
        {
            if (file.Extension.ToLower() == ".jpg" || file.Extension.ToLower() == ".bmp")
            {
                System.Drawing.Image image = System.Drawing.Image.FromFile(Server.MapPath(strBaseDir+strCategory+file.Name));
                System.Drawing.Imaging.ImageFormat imageFormat = image.RawFormat;
                if (image.Width > image.Height)
                {
                    System.Drawing.Image thumbImage = image.GetThumbnailImage(100, (100 * image.Height) / image.Width, null, new IntPtr());
                    thumbImage.Save(Server.MapPath(strBaseDir + strCategory + "thumb-" + file.Name),imageFormat);
                }
                else
                {
                    System.Drawing.Image thumbImage = image.GetThumbnailImage((100 * image.Width) / image.Height, 100, null, new IntPtr());
                    thumbImage.Save(Server.MapPath(strBaseDir + strCategory + "thumb-" + file.Name), imageFormat);
                }
            }
        }
    }
    protected void cmbCategories_SelectedIndexChanged(object sender, EventArgs e)
    {
        lblMessage.Text = "";
    }
}
